using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.CommonWeb;
using System.IO;
using System.Collections.Generic;

public partial class admin_ImagesBrowser : System.Web.UI.Page
{
    public static string ROOT_PATH = "../" + Constant.UPLOAD_FILE_PATH + "Files/";
    public static string FILES_MESSAGE = "FILES_MESSAGE";
    public static string FILES_MESSAGE_TYPE = "FILES_MESSAGE_TYPE";

    public string[] _ImageFilesList = new string[0];

    public long _TotalItem = 0;
    public string _CurrentPath = "/";
    //public string[] _FileList = new string[0];
    public string[] _DirectoryList = new string[0];
    //public string _Message = string.Empty;
    //public int _MessageType = 0; // 0: info, -1: error.

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Request.QueryString["file"] != null)
            {
                _CurrentPath = Request.QueryString["file"];
            }
            
            string rootServerPath = Server.MapPath(ROOT_PATH);
            string currentServerPath = Path.Combine(Server.MapPath(ROOT_PATH), this._CurrentPath.TrimStart('/').Replace("/", "\\"));

            if (!Directory.Exists(currentServerPath))
            {
                if (_CurrentPath != "/")
                {
                    Response.Redirect("ImagesBrowser.aspx", false);
                }
                return;
            }

            _DirectoryList = Directory.GetDirectories(currentServerPath);
            if (_DirectoryList != null)
            {
                for (int i = 0; i < _DirectoryList.Length; i++)
                {
                    _DirectoryList[i] = Path.GetFileName(_DirectoryList[i]);
                }
            }
            _ImageFilesList = this.GetImageList(currentServerPath);
        }
        catch
        {
        }
    }

    public string[] GetImageList(string folderPath)
    {
        SortedList<string, string> imageList = new SortedList<string, string>();
        string[] filters = Constant.IMAGE_FILE_EXTENTIONS.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string filter in filters)
        {
            string[] files = Directory.GetFiles(folderPath, filter);
            if (files != null && files.Length > 0)
            {
                foreach (string file in files)
                {
                    imageList.Add(file, file);
                }
            }
        }

        string[] ret = new string[imageList.Count];
        imageList.Values.CopyTo(ret, 0);
        return ret;
    }

    public string ParentPath
    {
        get
        {
            if (_CurrentPath == "/")
            {
                return null;
            }

            string parentPath = _CurrentPath.Substring(0, _CurrentPath.Length - 1);
            parentPath = parentPath.Substring(0, parentPath.LastIndexOf("/") + 1);

            return parentPath;
        }
    }
}
